home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / ip / manage / snmp / cmu-snmp1.0 / apps / snmpgetnext.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-19  |  4.1 KB  |  143 lines

  1. /*
  2.  * snmpgetnext.c - send snmp GETNEXT requests to a network entity.
  3.  *
  4.  */
  5. /***********************************************************
  6.     Copyright 1988, 1989 by Carnegie Mellon University
  7.  
  8.                       All Rights Reserved
  9.  
  10. Permission to use, copy, modify, and distribute this software and its 
  11. documentation for any purpose and without fee is hereby granted, 
  12. provided that the above copyright notice appear in all copies and that
  13. both that copyright notice and this permission notice appear in 
  14. supporting documentation, and that the name of CMU not be
  15. used in advertising or publicity pertaining to distribution of the
  16. software without specific, written prior permission.  
  17.  
  18. CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  19. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  20. CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  21. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  22. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  23. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  24. SOFTWARE.
  25. ******************************************************************/
  26. #include <sys/types.h>
  27. #include <netinet/in.h>
  28. #include <stdio.h>
  29.  
  30. #include "snmp.h"
  31. #include "snmp_impl.h"
  32. #include "asn1.h"
  33. #include "snmp_api.h"
  34. #include "snmp_client.h"
  35.  
  36. extern int  errno;
  37. int    snmp_dump_packet = 0;
  38.  
  39. main(argc, argv)
  40.     int        argc;
  41.     char    *argv[];
  42. {
  43.     struct snmp_session session, *ss;
  44.     struct snmp_pdu *pdu, *response;
  45.     struct variable_list *vars;
  46.     int    arg;
  47.     char *gateway = NULL;
  48.     char *community = NULL;
  49.     int    count, current_name = 0;
  50.     char *names[128];
  51.     oid name[MAX_NAME_LEN];
  52.     int name_length;
  53.     int status;
  54.  
  55.     init_mib();
  56.     /*
  57.      * usage: snmpgetnext gateway-name community-name object-id-list
  58.      */
  59.     for(arg = 1; arg < argc; arg++){
  60.     if (argv[arg][0] == '-'){
  61.         switch(argv[arg][1]){
  62.         case 'd':
  63.             snmp_dump_packet++;
  64.             break;
  65.         default:
  66.             printf("invalid option: -%c\n", argv[arg][1]);
  67.             break;
  68.         }
  69.         continue;
  70.     }
  71.     if (gateway == NULL){
  72.         gateway = argv[arg];
  73.     } else if (community == NULL){
  74.         community = argv[arg]; 
  75.     } else {
  76.         names[current_name++] = argv[arg];
  77.     }
  78.     }
  79.  
  80.     if (!(gateway && community && current_name > 0)){
  81.     printf("usage: snmpgetnext gateway-name community-name object-identifier [object-identifier ...]\n");
  82.     exit(1);
  83.     }
  84.  
  85.     bzero((char *)&session, sizeof(struct snmp_session));
  86.     session.peername = gateway;
  87.     session.community = (u_char *)community;
  88.     session.community_len = strlen((char *)community);
  89.     session.retries = SNMP_DEFAULT_RETRIES;
  90.     session.timeout = SNMP_DEFAULT_TIMEOUT;
  91.     session.authenticator = NULL;
  92.     snmp_synch_setup(&session);
  93.     ss = snmp_open(&session);
  94.     if (ss == NULL){
  95.     printf("Couldn't open snmp\n");
  96.     exit(-1);
  97.     }
  98.  
  99.     pdu = snmp_pdu_create(GETNEXT_REQ_MSG);
  100.  
  101.     for(count = 0; count < current_name; count++){
  102.     name_length = MAX_NAME_LEN;
  103.     if (!read_objid(names[count], name, &name_length)){
  104.         printf("Invalid object identifier: %s\n", names[count]);
  105.     }
  106.     
  107.     snmp_add_null_var(pdu, name, name_length);
  108.     }
  109.  
  110.     status = snmp_synch_response(ss, pdu, &response);
  111.     if (status == STAT_SUCCESS){
  112.     if (response->errstat == SNMP_ERR_NOERROR){
  113.         for(vars = response->variables; vars; vars = vars->next_variable)
  114.         print_variable(vars->name, vars->name_length, vars);
  115.     } else {
  116.         if (response->errstat == SNMP_ERR_NOSUCHNAME){
  117.         printf("You have reached the end of the MIB.\n");
  118.         } else {
  119.         printf("Error in packet.\nReason: %s\n", snmp_errstring(response->errstat));
  120.         if (response->errstat == SNMP_ERR_NOSUCHNAME){
  121.             printf("This name doesn't exist: ");
  122.             for(count = 1, vars = response->variables; vars && count != response->errindex;
  123.             vars = vars->next_variable, count++)
  124.                 ;
  125.             if (vars)
  126.             print_objid(vars->name, vars->name_length);
  127.             printf("\n");
  128.         }
  129.         }
  130.     }
  131.  
  132.     } else if (status == STAT_TIMEOUT){
  133.     printf("No Response from %s\n", gateway);
  134.     } else {    /* status == STAT_ERROR */
  135.     printf("An error occurred, Quitting\n");
  136.     }
  137.  
  138.     if (response)
  139.     snmp_free_pdu(response);
  140.     snmp_close(ss);
  141. }
  142.  
  143.